<?php
require_once 'config.php';

class CheckImage{
	
    public function __construct ()
    {
        $this->db = mysql_connect(DB_HOST, DB_USER, DB_PWD) or die('Could not connect: ' . mysql_error());
        echo DB_PREFIX . '_' . DB_NAME;
        mysql_select_db(DB_PREFIX . '_' . DB_NAME) or die('Could not select database');
        mysql_query('SET NAMES utf8');
        echo 'Connected successfully';
        echo "\n";
        echo "Please waiting ...\n";    	
    }
    
    public function check(){
    	$thumbnailPath = THUMBNAIL_P_PATH;
    	$sql = "select pi.*,p.title from product_images pi
    	       left outer join products p on p.id = pi.product_id
    	       where pi.is_delete = 0 and pi.product_id is not null ";
    	$result = mysql_query($sql) or die('Query failed: ' . mysql_error());
        $string = "<table border='1'>
                   <tr>
                       <td>Product Id</td>
                       <td>Title</td>
                       <td>Item Id</td>
                       <td>Name</td>
                       <td>Path</td>
                       <td>Exist</td>
                   </tr>";
    	while ($productImage = mysql_fetch_array($result, MYSQL_ASSOC)) {
    		$productId = $productImage['product_id'];
    		$itemId = $productImage['item_id'];
    		$name = $productImage['name'];
    		$folder = $productImage['folder'];
    		$title = $productImage['title'];
    		$imagePath = $thumbnailPath . $folder . $name . "_800.jpg";
    		if(!file_exists($imagePath)){
    			$string .= "<tr>";
    			$string .= "<td>$productId</td>
    			            <td>$title</td>
                            <td>$itemId</td>
                            <td>$name</td>
                            <td>$imagePath</td>
                            <td>N</td>
                           </tr>";
    			$string .= "</tr>";
    		}
    	}
    	$string .= "</table>";
        
        $file = '/opt/check_image_log.htm';
        $hf = fopen($file, 'w+');
        fwrite($hf, $string);
        fclose($hf);
        echo 'Please check log file' . $file;    	
    }
    
}

$checkImage = new CheckImage();
$checkImage->check();